package com.zmm.book.keep.service.impl;

import com.zmm.book.keep.dao.BillDao;
import com.zmm.book.keep.model.Bill;
import com.zmm.book.keep.service.BillService;
import com.zmm.book.keep.vo.BillConsumeDetailVo;
import com.zmm.book.keep.vo.BillResultVo;
import com.zmm.book.keep.vo.ConsumeCollectVo;
import com.zmm.book.keep.vo.ConsumeDetailVo;
import com.zmm.book.keep.vo.DetailParamVo;
import com.zmm.common.base.model.Query;
import com.zmm.util.time.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author 900045
 * @description:
 * @name BillServiceImpl
 * @date By 2021-09-28 10:12:57
 */
@Service
public class BillServiceImpl implements BillService {

	@Resource
	private BillDao billDao;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean saveBill(Bill bill) {
		Calendar calendar = Calendar.getInstance();
		Date createTime = calendar.getTime();
		// remark 2021-10-08 将date 改为由前端填写
		// String date = DateUtil.getNow("yyyy-MM-dd")
		// bill.setDate(date)
		bill.setCreateTime(createTime);
		Integer insert = billDao.insertBill(bill);
		if (insert > 0){
			return true;
		} else {
			return false;
		}
	}


	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean modifyBill(Bill bill) {
		Calendar calendar = Calendar.getInstance();
		Date updateTime = calendar.getTime();
		bill.setUpdateTime(updateTime);
		Integer update = billDao.updateBill(bill);
		if (update > 0){
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<Bill> findAllBill() {
		return null;
	}

	@Override
	public Bill findOneBill(Integer recordId) {
		return billDao.selectOneBill(recordId);
	}

	@Override
	public void queryBillList(Query<BillResultVo> queryBill) {
		Integer total = billDao.countBillList(queryBill);
		if (total > 0) {
			queryBill.setTotal(total);
			List<BillResultVo> searchBillList = billDao.searchBillList(queryBill);
			queryBill.setResults(searchBillList);
		}
	}

	@Override
	public BillConsumeDetailVo findBillDetail(Integer scope, DetailParamVo detailParamVo) {
		BillConsumeDetailVo billConsumeDetailVo = new BillConsumeDetailVo();
		
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DATE);
		switch (scope) {
			case 1:
				// 默认查询
				
				String date = year + "-" + month + "-%";
				BigDecimal sumConsume = billDao.totalConsumeByDate(detailParamVo,date);
				billConsumeDetailVo.setSumConsume(sumConsume);

				ConsumeCollectVo consumeCollectVo = new ConsumeCollectVo();
				
				// 支出
				BigDecimal outMoney = billDao.totalConsumeByModeAndDate(detailParamVo,1,date);
				consumeCollectVo.setOutMoney(outMoney);

				// 收入
				BigDecimal inMoney = billDao.totalConsumeByModeAndDate(detailParamVo,2,date);
				consumeCollectVo.setInMoney(inMoney);
				
				billConsumeDetailVo.setConsumeCollectVo(consumeCollectVo);

				List<ConsumeDetailVo> consumeDetailVos = billDao.consumeDetailByDate(date,detailParamVo);
				billConsumeDetailVo.setConsumeDetailVos(consumeDetailVos);
				
				break;
			case 2:
				// 截止到结束时间
				// 当前日期 startTime
				detailParamVo.setStartTime(year + "-" + month + "-" + day);
				// DATE1>=DATE2,返回true
				if (DateUtil.compareDate(detailParamVo.getEndTime(),detailParamVo.getStartTime())){
					packDate(billConsumeDetailVo,detailParamVo,detailParamVo.getStartTime(),detailParamVo.getEndTime());
				}
				break;
			case 3:
				// 开始时间 ~ 当前时间
				detailParamVo.setEndTime(year + "-" + month + "-" + day);
				if (DateUtil.compareDate(detailParamVo.getEndTime(),detailParamVo.getStartTime())){
					packDate(billConsumeDetailVo,detailParamVo,detailParamVo.getStartTime(),detailParamVo.getEndTime());
				}
				break;
			case 4:
				// 开始时间 ~ 结束时间

				packDate(billConsumeDetailVo,detailParamVo,detailParamVo.getStartTime(),detailParamVo.getEndTime());
				break;
			default:
				return null;
		}
		return billConsumeDetailVo;
	}
	
	/**
	 * 封装查询的条件
	 * @author: 900045
	 * @date: 2021-10-09 15:36:07
	 * @throws 
	 * @param billConsumeDetailVo: 
	 * @param detailParamVo:
	 * @param startTime: 
	 * @param endTime: 
	 * @return: com.zmm.book.keep.vo.BillConsumeDetailVo
	 **/
	private BillConsumeDetailVo packDate(BillConsumeDetailVo billConsumeDetailVo,DetailParamVo detailParamVo,String startTime,String endTime){
		BigDecimal sumConsume = billDao.totalConsumeByBetweenDate(detailParamVo,startTime,endTime);

		ConsumeCollectVo consumeCollectVo = new ConsumeCollectVo();
		
		BigDecimal outMoney = billDao.totalConsumeByModeAndBetweenDate(1,detailParamVo,startTime,endTime);

		BigDecimal inMoney = billDao.totalConsumeByModeAndBetweenDate(2,detailParamVo,startTime,endTime);
		consumeCollectVo.setOutMoney(outMoney);
		consumeCollectVo.setInMoney(inMoney);
		
		List<ConsumeDetailVo> consumeDetailVos = billDao.consumeDetailByBetweenDate(startTime,endTime,detailParamVo);
		billConsumeDetailVo.setSumConsume(sumConsume);
		billConsumeDetailVo.setConsumeCollectVo(consumeCollectVo);
		billConsumeDetailVo.setConsumeDetailVos(consumeDetailVos);
		return billConsumeDetailVo;
	}
}
